package com.atwisdom.star.service.business;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.SpringContextUtil;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;
import com.atwisdom.star.core.dsl.modelClass.hier.Impl.HierInstanceDslServiceImpl;
import com.atwisdom.star.service.util.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
//@RequiredArgsConstructor
public abstract class Business {
    protected final HierInstanceDslServiceImpl hierInstanceDslServiceImpl = SpringContextUtil.getBean(HierInstanceDslServiceImpl.class);

    protected final LogicUtil logicUtil = SpringContextUtil.getBean(LogicUtil.class);

    protected final ResourceUtil resourceUtil = SpringContextUtil.getBean(ResourceUtil.class);

    protected final ConsumeUtil consumeUtil  = SpringContextUtil.getBean(ConsumeUtil.class);

    protected final StaticUtil staticUtil  = SpringContextUtil.getBean(StaticUtil.class);

    protected final TankUtil tankUtil  = SpringContextUtil.getBean(TankUtil.class);

    protected final TwinsUtil twinUtil = SpringContextUtil.getBean(TwinsUtil.class);

    protected final RoutingUtil routingUtil = SpringContextUtil.getBean(RoutingUtil.class);

    protected final WorkCenterUtil workCenterUtil = SpringContextUtil.getBean(WorkCenterUtil.class);


    /**
     * 根据parentId生成hier数据
     * @param model
     * @param className
     * @param containerId
     * @param parentId
     * @param hierList
     */
    public void createBatchHier(String model, String className, Long containerId,String parentId,List<JSONObject> hierList){
        if(StrUtil.isEmpty(parentId)){
            return;
        }
        List<JSONObject> list = hierList.stream().filter(item->parentId.equals(item.getString("parentId"))).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(list)){
            return;
        }
        hierInstanceDslServiceImpl.addChild(model,className,containerId,parentId,list);
        list.forEach(item->{
            createBatchHier(model,className,containerId,item.getString("nodeId"),hierList);
        });
    }

    /**
     * 根据parentId数组生成hier数据
     * @param model
     * @param className
     * @param containerId
     * @param parentIds
     * @param hierList
     */
    public void createBatchHier(String model, String className, Long containerId, List<String> parentIds, List<JSONObject> hierList){
        parentIds.forEach(item->{
            createBatchHier(model,className,containerId,item,hierList);
        });
    }

    /**
     * 添加子节点
     * @param model 模型
     * @param className 类名
     * @param containerId 主id
     * @param parentId 父id
     * @param hierJson 节点json
     */
    public void addChild(String model, String className, Long containerId, String parentId, JSONObject hierJson) {
        hierInstanceDslServiceImpl.addChild(model,className,containerId,parentId, Arrays.asList(hierJson));
    }

    public List<String> getParentIdList(List<HierInstance> hierList){
        List<String> listString = new ArrayList<>();
        hierList.stream().forEach(item->{
            List<HierInstance> list = hierList.stream().filter(item1->item1.getNodeId().equals(item.getParentId())).collect(Collectors.toList());
            if(list.size()==0){
                listString.add(item.getNodeId());
            }
        });
        return listString;
    }
}
